//
//  MessageDetailVC.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/7/10.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "MessageDetailVC.h"
#import "MessageDetailCell.h"
#import "MessageDM.h"
#import "BdTV.h"
#import "config.h"
#import "AppDelegate.h"



@interface MessageDetailVC ()<UITableViewDataSource,UITableViewDelegate,MessageDelegate>
@property (strong, nonatomic) NSArray *dataArray;
@end

@implementation MessageDetailVC



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [AppDelegate sharedAppDelegate].delegate = self;
    
    
    [MessageDM setMsgType:self.type numberNotRead:0];
    
}


-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [AppDelegate sharedAppDelegate].delegate = nil;
    
     [MessageDM setMsgType:self.type numberNotRead:0];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

-(void)initView{
    [super initView];
    
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title setMianTitle:self.titleStr];
    [[title backBtn]setTag:11];
    [[title backBtn]addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    title.doneBtn.hidden = YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    
    
    [self refreshData];
    
}


-(void)refreshData{
    
    [self.nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) baseSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        baseSelf.dataArray = [MessageDM getMessage:baseSelf.type];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            
            if (baseSelf.dataArray.count==0) {
                [baseSelf.nodataCell setState:nodataCellStateNodata];
            }else{
                [baseSelf.tableView reloadData];
            }
        });
    });
}



-(void)btnOnClick:(UIButton *)sender{
    
    if(sender.tag==11){
        [self.navigationController popViewControllerAnimated:YES];
    }
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.dataArray.count > 0) {
        
        return self.dataArray.count;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataArray.count==0) {
        
        return self.nodataCell;
    }
    
    MessageDetailCell *cell = [MessageDetailCell cellWithTableView:tableView];
    [cell setModel:[self.dataArray objectAtIndex:indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.dataArray.count > 0) {
        
        return  [MessageDetailCell getHeightWithTableView:tableView Model:[self.dataArray objectAtIndex:indexPath.row]];
    }
    return tableView.frame.size.height;
}


#pragma mark message receive delegate
- (void) messageReceived:(MessageModel *)msg{
    
    [self refreshData];
}

@end
